package controllers

import (
	"encoding/json"
	"gif_project/db"
	"gif_project/models"
	"gif_project/utils"
	"github.com/astaxie/beego"
	_ "github.com/astaxie/beego/orm"
)

type ArticleListController struct {
	beego.Controller
}

// @Title api_getArticleList
// @Description 获取帖子列表
// @Param	body	body	models.ArticleListReq	true	"body for ArticleListReq content"
// @Success 200 {object} models.BaseResp
// @Failure 500 {object} models.BaseResp
// @router /api_getArticleList [post]
func (articleListController *ArticleListController) GetArticleList() {
	var articleListReq models.ArticleListReq
	err := json.Unmarshal(articleListController.Ctx.Input.RequestBody, &articleListReq)
	utils.CheckErr(err)
	page, err := db.GetArticleWithPage(articleListReq.PageNo, articleListReq.PageSize)
	utils.ResBaseJson(err, page, &articleListController.Controller)
}

// @Title api_getArticleType
// @Description 获取帖子所有分类
// @Success 200 {object} models.BaseResp
// @Failure 500 {object} models.BaseResp
// @router /api_getArticleType [post]
func (articleListController *ArticleListController) GetArticleType() {
	groupTypes := db.GetArticleGroupByType()
	utils.ResBaseJson(nil, groupTypes, &articleListController.Controller)
}

// @Title api_getArticleTypePage
// @Description 获取帖子指定分类列表
// @Param	body	body	models.ArticleGroupTypeReq	true	"body for ArticleGroupTypeReq content"
// @Success 200 {object} models.BaseResp
// @Failure 500 {object} models.BaseResp
// @router /api_getArticleTypePage [post]
func (articleListController *ArticleListController) GetArticleTypePage() {
	var articleGroupTypeReq models.ArticleGroupTypeReq
	err := json.Unmarshal(articleListController.Ctx.Input.RequestBody, &articleGroupTypeReq)
	utils.CheckErr(err)
	groupTypes, err := db.GetArticleGroupByTypePage(articleGroupTypeReq.PageNo, articleGroupTypeReq.PageSize, articleGroupTypeReq.TypeName)
	utils.ResBaseJson(err, groupTypes, &articleListController.Controller)
}
